package com.craftplay.dao;

import com.craftplay.model.AdminUser;
import com.craftplay.model.Content;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.type.JdbcType;

import java.util.List;

@Mapper
public interface PageMapper {
    @Insert({
            "INSERT INTO admin_user (`userName`, `password`, `loginTime`)" +
                    " VALUES ( #{userName,jdbcType=VARCHAR}, #{password,jdbcType=VARCHAR},#{loginTime,jdbcType=INTEGER})"
    })
    int adminUserInsert(AdminUser adminUser);

    @Select({
            "select",
            "id, userName, password, loginTime",
            "from admin_user"
    })
    List<AdminUser> adminUserListAll();

    @Select({
            "select",
            "id, userName, password, loginTime",
            "from admin_user",
            "where userName = #{userName,jdbcType=VARCHAR}"
    })
    @Results({
            @Result(column = "id", property = "id", jdbcType = JdbcType.INTEGER, id = true),
            @Result(column = "userName", property = "userName", jdbcType = JdbcType.VARCHAR),
            @Result(column = "password", property = "password", jdbcType = JdbcType.VARCHAR),
            @Result(column = "loginTime", property = "loginTime", jdbcType = JdbcType.BIGINT),
    })
    List<AdminUser> adminUserSelectByUserName(String userName);


    @Update({
            "update admin_user",
            "set userName = #{userName,jdbcType=INTEGER},",
            "password = #{password,jdbcType=VARCHAR},",
            "loginTime = #{loginTime,jdbcType=VARCHAR},",
            "where id = #{id,jdbcType=INTEGER}"
    })
    int adminUserUpdateByPrimaryKey(AdminUser adminUser);


    //查询满足某个条件的用户
    @Select({"<script>",
            "select",
            "* ",
            "from content",
            "where 1=1",
            "<if  test='type!=null' >",
            " and type = #{type}",
            "</if>",
            "</script>"
    })
    List<Content> getContentList(Content content);

    @Update({
            "update content",
            "set type = #{type,jdbcType=VARCHAR},",
            "word = #{word,jdbcType=VARCHAR},",
            "url = #{url,jdbcType=VARCHAR},",
            "picUrl = #{picUrl,jdbcType=VARCHAR},",
            "title = #{title,jdbcType=VARCHAR},",
            "content = #{content,jdbcType=VARCHAR}",
            "where id = #{id,jdbcType=INTEGER}"
    })
    int updateContent(Content content);
}